<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 0px;
            top: 0px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script type="module">
        // import MyEvent from './myevent.js';

        // let myevent = new MyEvent();
        let myevent = new EventTarget();
        let count = 1;
        function move(boxEle, target,dir) {
            var timer = setInterval(() => {
                let num = parseInt(getComputedStyle(boxEle, null)[dir]);
                // currentStyle  :IE 浏览器
                console.log(num);
                let speed = 10 * (target>num?1:-1);
                if (num === target) {
                    clearInterval(timer);
                    // cb();
                    // myevent.trigger("myevent"+count);
                    myevent.dispatchEvent(new CustomEvent("myevent"+count))
                    count++;
                } else {
                    boxEle.style[dir] = num + speed + "px";
                }
            }, 100)
        }

        let boxEle = document.querySelector(".box");
        // move(boxEle,300,"left");
        // myevent.addEvent("myevent1",function(){
        //     console.log("运动结果111");
        //     move(boxEle,300,"top")
        // })

        // myevent.addEvent("myevent2",function(){
        //     console.log("运动结束222")
        //     move(boxEle,0,"left")
        // })

        // myevent.addEvent("myevent3",function(){
        //     console.log("运动结束333")
        //     move(boxEle,0,"top")
        // })


        // myevent.addEvent("myevent4",function(){
        //     console.log("运动结束444")
        // })



        move(boxEle,300,"left");
        myevent.addEventListener("myevent1",function(){
            console.log("运动结果111");
            move(boxEle,300,"top")
        })

        myevent.addEventListener("myevent2",function(){
            console.log("运动结束222")
            move(boxEle,0,"left")
        })

        myevent.addEventListener("myevent3",function(){
            console.log("运动结束333")
            move(boxEle,0,"top")
        })


        myevent.addEventListener("myevent4",function(){
            console.log("运动结束444")
        })




        // 回调地狱 
        // move(boxEle,300,"left",function(){
        //     move(boxEle,300,"top",function(){
        //         move(boxEle,0,"left",function(){
        //             move(boxEle,0,"top",function(){
        //                 console.log('完成');
        //             })
        //         })
        //     })
        // })


    </script>
</body>

</html>